/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package empresa;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.classic.Session;

/**
 *
 * @author Sigifredo Escobar Gómez
 */
public class ConsultaEmpresa {

    Session session = null;

    public ConsultaEmpresa() {
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
    
    public List getEmpresa() {
        List<Empresa> empre = null;

        try {
            // org.hibernate.Transaction tx = session.beginTransaction();
            session.beginTransaction();
            Query q = session.createQuery("from Empresa as empresa");
            empre = (List<Empresa>) q.list();
        } catch(Exception e) {
            e.printStackTrace();
        }

        return empre;
    }

    public String guardar_empresa(Empresa em) {
        try {
            // org.hibernate.Transaction tx = session.beginTransaction();
            // Se obtiene la sesión
            Session s = HibernateUtil.getSessionFactory().getCurrentSession();
            s.beginTransaction();
            // Session s = HibernateUtil.getSessionFactory();
            // s.beginTransaction();
            s.save(em);
            s.getTransaction().commit();
            // session.save(em);
            // tx.commit();

            return "Empersa guardada: " + em.getRut() + " - " + em.getNombre() + " - " + em .getDireccion();

        } catch (Exception e) {
            e.printStackTrace();

            return "Error";
        }
    }
}
